<?php


namespace app\api\controller\v25;


use app\aliyun\controller\AliyunFilterWord;
use app\api\controller\v2\Auth;
use app\common\cachemodel\ZlcrAstr;
use app\common\cachemodel\ZlcrAstrAccount;
use app\common\cachemodel\ZlcrAstrAccountRecord;
use app\common\cachemodel\ZlcrAstrMessage;
use app\common\cachemodel\ZlcrContentCheck;
use app\common\cachemodel\ZlcrCustomReportGoods;
use app\common\cachemodel\ZlcrCustomReportQuestion;
use app\common\cachemodel\ZlcrCustomReportQuestionComment;
use app\common\cachemodel\ZlcrCustomReportQuestionInfo;
use app\common\cachemodel\ZlcrTag;
use app\common\lib\exception\ApiException;
use app\common\lib\SensorsData;
use app\common\lib\Util;
use Exception;
use think\Cache;
use think\Db;
use think\Env;
use think\response\Json;

class UserCustomReport extends Auth
{
    /**
     * 定制报告详情
     * @return Json
     * @throws Exception
     */
    public function customReportDetail(): Json
    {
        $id = input('id/s', '');
        if (!$id)
        {
            return show(0, '请求参数异常');
        }
        $questionModel = new ZlcrCustomReportQuestion();
        $question = $questionModel->getInfoById($id);
        if (empty($question) || (isset($question['uid']) && (int)$question['uid'] !== $this->user['id']))
        {
            return show(config('code.error'), '数据不存在');
        }
        $question['status'] = (int)$question['status'];
        //处理报告已读状态
        if (!$question['is_read'])
        {
            $questionModel->updateData($question['uid'], $id, ['is_read' => 1]);
            $questionModel->listDataDel($this->user['id'], [$id], 1);
        }

        //获取报告结果信息
        $questionResult = (new ZlcrCustomReportQuestionInfo())->getInfoById($id);
        $tips = '';
        $is_remark = 0;
        $tagList = null;
        switch ($question['status'])
        {
            case 1://问题待审核
            case 2://等待接单
            case 3://问题待解答
            case 4://解答待审核
            case 13://问题待解答（答案审核未通过)
                return show(config('code.error'), '报告还未出炉，请耐心等待～');
            case 5://已完成待评价
                $tagModel = new ZlcrTag();
                $tag = $tagModel->getLevelTag(2, 0);
                $tagList = $tagModel->getQuestionAllsResrt($tag);
            case 6://已评价待审核
            case 7://评价已审核完成
                $status = 3;//已完成
                //获取对应占星师资料
                $astr = (new ZlcrAstr())->getInfoById($question['astr_id']);
                //获取对应的评论信息
                $comment = (new ZlcrCustomReportQuestionComment())->getInfoById($id);
                //获取对应等级的标签信息
                $tags = [];
                //评论内容
                $remark_data = null;
                //问题被评论
                if ($comment)
                {
                    $is_remark = 1;
                    $tag = (new ZlcrTag())->getLevelTag(2, $comment['level']);
                    foreach ($tag as $item)
                    {
                        $tags[] = [
                            'tname' => $item['tag_name'],
                            'is_checked' => in_array($item['id'], explode(',', $comment['tid']), false) ? 1 : 0
                        ];
                    }
                    $remark_data = [
                        'level' => (int)$comment['level'],
                        'remark' => $comment['comment'],
                        'tags' => $tags,
                        'is_violation' => (int)$comment['status'] === 3 ? 1 : 0 // 是否违规
                    ];
                }
                $res_data = [
                    'astr_name' => $astr['nickname'],
                    'astr_avatar' => $astr['avatar'],
                    'content' => $questionResult['answer_content'],
                    'remark_data' => $remark_data
                ];

                $contactInfo = [ // 客服信息
                    'img' => Env::get('host.photo').'/custom_reports/img/img/contact_img.png',
                    'name' => 'zhunleapp2',
                    'time' => '工作日10:00-18:00',
                ];
                break;
            case 10://问题审核未通过失效
            case 11://订单派发超时失效
            case 12://占星师超时未完成失效
            case 14://占星师拒答两次失效
            case 15://取消订单，金额退回90%
            case 16://取消订单，金额全部退回
                $status = 4;//已失效
                $tips = '7个工作日内退款至原支付账户';
                if ($question['status'] === 10)
                {
                    $msg = '您好，该报告订单因提问内容不符合平台规范失效，退款金额将在７个工作日内原路返回，敬请谅解';
                } elseif ($question['status'] === 11)
                {
                    $msg = '您好，该报告因订单派发超时失效，退款金额将在７个工作日内原路返回，敬请谅解';
                } elseif ($question['status'] === 12)
                {
                    $msg = '您好，该报告订单因占星师超时未完成失效，退款金额将在７个工作日内原路返回，赔偿您月运免费卷已经发送至卡劵中心，带来的不便敬请谅解';
                } elseif ($question['status'] === 14)
                {
                    $msg = '您好，该报告订单因过多占星师反馈无法回复失效，退款金额将在７个工作日内原路返回，敬请谅解';
                } elseif ($question['status'] === 15)
                {
                    $tips = '7个工作日内退款至原支付账户';
                    $msg = '您好，该报告订单已为你取消，因占星师已在制作中扣除10%订单金额，望您谅解，退款金额将在７个工作日内原路返回，具体以支付渠道为准';
                } elseif ($question['status'] === 16)
                {
                    $tips = '7个工作日内退款至原支付账户';
                    $msg = '您好，该报告订单已为你取消，退款金额将在７个工作日内原路返回，具体以支付渠道为准';
                } else
                {
                    $msg = '您好，该报告订单因提问内容不符合平台规范失效，款项将在7个工作日内原渠道返回，敬请谅解';
                }
                $fail_info = [
                    'msg' => $msg,
                    'zhunle' => '准了占星团队',
                    'time' => $question['invalid_time'] ? date('Y.m.d H:i', $question['invalid_time']) : date('Y.m.d H:i'),
                ];
                break;
            default:
                return show(config('code.error'), '报告结果暂未出炉，请耐心等候');
        }
        if (isset($questionResult['supplement_info']) && $questionResult['supplement_info'])
        {
            $supplementInfo = \GuzzleHttp\json_decode($questionResult['supplement_info'], true);
        }
        if (isset($questionResult['recode_info']) && $questionResult['recode_info'])
        {
            $recodeInfo = \GuzzleHttp\json_decode($questionResult['recode_info'], true);
        }

        $data = [
            'is_merge' => $question['is_merge'],
            'rid1' => $questionResult['rid1'],
            'rid2' => $questionResult['rid2'],
            'question_name' => $question['question_name'],
            'create_time' => date('Y.m.d H:i', $question['create_time']),
            'status' => $status,
            'tips' => $tips,
            'fail_info' => $fail_info ?? null,
            'is_remark' => $is_remark,
            'res_data' => $res_data ?? null,
            'tag_list' => $tagList,
            'extend_infos' => $supplementInfo ?? [],
            'contact_info' => $contactInfo ?? [],
            'rid1_name' => $recodeInfo['recode1']['name']??'',
            'rid2_name' => $recodeInfo['recode2']['name']??'',
        ];
        return show(200, '请求成功', $data);
    }
}
